package com.salton123.colorflydemo.util.floodfill;

import android.graphics.Bitmap;
import android.graphics.Color;

import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

public class OpencvFloodFill implements IFloodFillMethod {
    private Mat mat;

    public OpencvFloodFill() {
        mat = new Mat();
    }

    @Override
    public void floodFill(Bitmap bitmap, int x, int y, int color, int tolerance) {
        Utils.bitmapToMat(bitmap, mat);
        Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGBA2RGB);

        // define the seed point
        Point seedPoint = new Point(x, y);
        // flood fill with red
        Imgproc.floodFill(
                mat,
                new Mat(),
                seedPoint,
                new Scalar(Color.red(color), Color.green(color), Color.blue(color), Color.alpha(color)), // LTGRAY
                new Rect(),
                new Scalar(0, 0, 0),
                new Scalar(0, 0, 0),
                4
        );

        // show the output
        Utils.matToBitmap(mat, bitmap);
    }
}
